<?php

class ConexionBD {
    private $usuario;
    private $password;
    private $host;
    private $conexion;
    private $database;


    public function __construct(){       
        $handle = fopen("..\data\bdconnect", "r");
        $cont = 0;
        if ($handle) {
            while (($line = fgets($handle)) !== false) {
                if($cont == 0){
                     $this->usuario = trim($line);
                }elseif($cont == 1){
                    $this->password = trim($line);
                }elseif($cont == 2){
                    $this->host = trim($line);
                }elseif($cont == 3){
                    $this->database = trim($line);
                    
                }
                $cont = $cont + 1;
            }
        } else {
            echo "Error al abrir el archivo de BD";
        }
        fclose($handle);
    }
    

    public function abrirConexion(){
        
        $this->conexion = mysql_connect($this->host,  $this->usuario,  $this->password);
        mysql_select_db($this->database,$this->conexion);

        // Check connection
        if (!$this->conexion)
        {
          return "Fallo al conectar a la base de datos: " . mysql_error();
        }
        
        return "Conexión Exitosa";
    }
    
    public function genericSimpleQuery($query,$error){
        $this->abrirConexion();
        $salida = mysql_query($query, $this->getConexion());
        if(!$salida){
             $this->cerrarConexion();
             return 0;
        }
        $this->cerrarConexion();
        return 1;
    }
    
    public function cerrarConexion(){
        mysql_close($this->conexion);
        
        return "Conexción Cerrada";
    }
    
    public function getConexion() {
        return $this->conexion;
    }
    
    public function getUsuario() {
        return $this->usuario;
    }

    public function getPassword() {
        return $this->password;
    }

    public function getHost() {
        return $this->host;
    }

    public function getDatabase() {
        return $this->database;
    }

    public function setUsuario($usuario) {
        $this->usuario = $usuario;
    }

    public function setPassword($password) {
        $this->password = $password;
    }

    public function setHost($host) {
        $this->host = $host;
    }

    public function setDatabase($database) {
        $this->database = $database;
    }


}

?>
